//////////////////////////////////////////////////////////////////////////////////
//                                                                              //
//  This file is part of the buola project (https://code.google.com/p/buola/).  //
//                                                                              //
//  Copyright(c) 2007-2012 Xavi Gratal                                          //
//  gratal AT gmail DOT com                                                     //
//                                                                              //
//  Buola is free software: you can redistribute it and/or modify               //
//  it under the terms of the GNU General Public License as published by        //
//  the Free Software Foundation, either version 3 of the License, or           //
//  (at your option) any later version.                                         //
//                                                                              //
//  Buola is distributed in the hope that it will be useful,                    //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of              //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               //
//  GNU General Public License for more details.                                //
//                                                                              //
//  You should have received a copy of the GNU General Public License           //
//  along with buola.  If not, see <http://www.gnu.org/licenses/>.              //
//                                                                              //
//////////////////////////////////////////////////////////////////////////////////

#ifndef _APPS_BUSH_CCOMMAND_H_
#define _APPS_BUSH_CCOMMAND_H_

#include <buola/buola.h>
#include "cbuiltin.h"
#include "credirection.h"
#include <buola/io/cbufferstream.h>
#include <buola/os/cprocess.h>

namespace buola { namespace bush {

struct CCommand;
typedef intrusive_ptr<CCommand> PCommand;
    
struct CCommand : public UShared
{
    CCommand(const std::vector<CParsedWord> &pV)
        :   mParsedWords(pV)
        ,   mOutput(nullptr)
    {
    }
    
    CCommand(std::vector<CParsedWord> &&pV)
        :   mParsedWords(std::move(pV))
        ,   mOutput(nullptr)
    {
    }

    void Add(PRedirection pRedirection)
    {
        mRedirections.push_back(pRedirection);
    }

    void Prepend(PRedirection pRedirection)
    {
        mRedirections.insert(mRedirections.begin(),pRedirection);
    }

    void SetOutput(std::string *pOut);

    void ExpandArgs();
    void CreateProcess(PCommand pNext);
    int Launch(int pPGID,bool pFG);
    void Wait();
    CProcess::EStatus GetStatus();
    int GetExitCode();
    int GetExitSignal();
    bool Signalled();

    bool IsBuiltin()    {   return mBuiltin;    }
    
public:
    std::vector<CParsedWord> mParsedWords;
    std::vector<CWord> mWords;

    std::string mCommand;
    std::vector<std::string> mArgs;
    
    std::vector<PRedirection> mRedirections;
    
    std::unique_ptr<CProcess> mProcess;
    CBuiltin *mBuiltin;
    
    std::string *mOutput;
    io::PBufferStream mOutStream;
    
    CCommand *mNextCommand;
    int mPGID;
};


/*namespace bush*/ } /*namespace buola*/ }

#endif
